#include"threadPool.h"
pthread_key_t key;
void destructor(void * value){
    free(value);
}
int main(int argc, char *argv[]){
    pthread_key_create(&key, destructor);
    ARGS_CHECK(argc,4);
    threadPool_t threadPool;
    int workerNum = atoi(argv[3]);
    threadPoolInit(&threadPool, workerNum);
    makeWorker(&threadPool);

    //tcp
    int sockfd = tcpInit(argv[1], argv[2]);

    //epoll
    int epfd = epoll_create(1);
    epollAdd(epfd,sockfd);

    struct epoll_event readyset[1024];
    while(1){
        int readynum = epoll_wait(epfd,readyset,1024,-1);
        for(int i=0; i<readynum; ++i){
            if(readyset[i].data.fd == sockfd){
                int netfd = accept(sockfd,NULL,NULL);
                printf("I am master, I got a netfd = %d\n",netfd);
                pthread_mutex_lock(&threadPool.mutex);

                //登录验证
                //把验证通过的放入任务队列
                enQueue(&threadPool.taskQueue,netfd);
                pthread_cond_broadcast(&threadPool.cond);
                pthread_mutex_unlock(&threadPool.mutex);
            }

        }
    }
    return 0;

}
